package meijuli.com.join51.business.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.adapter.CustomAdapter;
import meijuli.com.join51.business.adapter.CustomHolder;
import com.jhx.common.http.ApiManager;
import com.jhx.common.http.HttpRequest;
import com.jhx.common.http.HttpResponse;
import com.jhx.common.http.ResponseCallback;
import meijuli.com.join51.business.model.Commodity;
import meijuli.com.join51.business.service.ApiService;
import meijuli.com.join51.business.ui.widget.CustomItemDecoration;
import com.jhx.common.utils.DisplayUtil;
import com.jhx.common.utils.ToastUtil;

import java.util.ArrayList;
import java.util.Collections;
import retrofit2.Call;

/**
 * 商品详情页
 *
 * @author jack 2017-04-25 17:12
 */
public class CommodityDetailActivity extends BaseActivity {
    private static final String KEY_COMMODITY = "key_commodity";
    private static final String KEY_FROM = "key_from";
    private Call<HttpResponse<Commodity>> mCall;
    private Commodity mCommodity;
    TextView mTextView;
    Toolbar mToolbar;
    TextView mCategoryText;
    TextView mNameText;
    TextView mPriceText;
    TextView mCostPriceText;
    TextView mDescText;
    RecyclerView mImgRecyclerView;
    TextView mTextStatus;
    TextView mRepertoryText;

    public static void start(Activity activity, Commodity commodity, int from) {
        Intent intent = new Intent(activity, CommodityDetailActivity.class);
        intent.putExtra(KEY_FROM, from);
        intent.putExtra(KEY_COMMODITY, commodity);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_detail);
        mTextView = findViewById(R.id.toolbar_title);
        mToolbar = findViewById(R.id.toolbar);
        mCategoryText = findViewById(R.id.text_category);
        mNameText = findViewById(R.id.text_name);
        mPriceText = findViewById(R.id.text_price);
        mCostPriceText = findViewById(R.id.text_costprice);
        mDescText = findViewById(R.id.text_description);
        mImgRecyclerView = findViewById(R.id.rv_img);
        mTextStatus = findViewById(R.id.text_status);
        mRepertoryText = findViewById(R.id.text_repertory);
        initToolbar();
        mCommodity = (Commodity) getIntent().getSerializableExtra(KEY_COMMODITY);
        if (mCommodity == null) {
            ToastUtil.getInstance().showToast(this, "获取数据失败");
            return;
        }
        requestData();
    }

    private void requestData() {
        showLoadingDialog();
        String goodsId = null;
        mCall = ApiManager.get().getApiService(ApiService.class).getCommodity(goodsId);
        HttpRequest.getRequest().autoRecycle(this).start(mCall, new ResponseCallback<Commodity>() {

            @Override
            public void onSuccess(String url, Commodity data) {
                dismissDialog();
                mCommodity = data;
                if (mCommodity == null) {
                    ToastUtil.getInstance().showToast(CommodityDetailActivity.this, "获取数据失败");
                    return;
                }
                setData();
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                super.onError(url, errorCode, errorMsg);
                dismissDialog();
            }
        });
    }

    private void setData() {
        mTextStatus.setText(mCommodity.getStatus() == 1 ? "上架" : "下架");
        mCategoryText.setText(mCommodity.getOneCategoryName());
        mPriceText.setText(mCommodity.getPrice() + "元");
        mCostPriceText.setText(mCommodity.getFinalPrice() + "元");
        mNameText.setText(mCommodity.getGoodsName());
        mRepertoryText.setText(mCommodity.getSales() + "");
        if (TextUtils.isEmpty(mCommodity.getGoodsDesc())) {
            mDescText.setText("暂无描述~");
        } else {
            mDescText.setText(mCommodity.getGoodsDesc());
        }
        String picture = mCommodity.getPicture();
        if (picture == null) {
            return;
        }
        String[] urlArray = picture.split(",");
        final ArrayList<String> urlList = new ArrayList<>();
        Collections.addAll(urlList, urlArray);
        mImgRecyclerView.setLayoutManager(new GridLayoutManager(CommodityDetailActivity.this, 2));
        mImgRecyclerView.addItemDecoration(new CustomItemDecoration(DisplayUtil.dip2px(this, 1),
                LinearLayoutManager.HORIZONTAL));
        CustomAdapter mAdapter = new CustomAdapter<String>(urlList,
                CommodityDetailActivity.this,
                R.layout.item_image,
                null) {
            @Override
            public void onBindData(CustomHolder holder, int position, String url) {
                ImageView imageView = holder.findView(R.id.bankCardIco);
                Glide.with(CommodityDetailActivity.this).load(url).error(R.mipmap.img_default).into(imageView);
            }
        };
        mImgRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new CustomAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                Intent intent = new Intent(CommodityDetailActivity.this, PicturePreviewActivity.class);
                intent.putStringArrayListExtra(PicturePreviewActivity.KEY_DATA, urlList);
                intent.putExtra(PicturePreviewActivity.KEY_INDEX, position);
                startActivity(intent);
            }
        });
    }

    private void initToolbar() {
        mTextView.setText("商品详情");
        mToolbar.setNavigationIcon(R.mipmap.icon_back_white);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

}
